home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8815 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: crc-news.doc.ca!usenet
  2. From: don@mars.dgrc.doc.ca (Donald McLachlan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: f;oating point precision
  5. Date: 6 Mar 1996 14:33:41 GMT
  6. Organization: The Communications Research Centre
  7. Message-ID: <4hk7o5$gmk@crc-news.doc.ca>
  8. References: <4hit8c$8lm@news.microsoft.com>
  9. Reply-To: don@mars.dgrc.doc.ca
  10. NNTP-Posting-Host: gizmo.dgrc.doc.ca
  11.  
  12. An even simpler example of the problem.  The nice simple number .1 base10 cannot
  13. be *exactly* represented as a binary number.  My simple way of converting from
  14. base10 decimals to binary is:
  15.  
  16. 1. initialise the result string with a decimal point.
  17.  
  18.     .
  19.  
  20. 2. take the base10 number (.1) and multiply by 2 (= 0.2) and append the digit on the
  21.    left of the decimal point to the result in step 1.  
  22.  
  23.     .0
  24.  
  25. 3. Remove the digit to the left of the decimal point (.2).  If the number is still
  26.    != 0, repeat steps 2 and 3.
  27.  
  28. For .1 base10, the process is:
  29.  
  30.     math            result
  31.     .1 * 2 = 0.2        .0
  32.     .2 * 2 = 0.4        .00    <----------------
  33.     .4 * 2 = 0.8        .000            |
  34.     .8 * 2 = 1.6        .0001            |
  35.     .6 * 2 = 1.2        .00011            |
  36.     .2 * 2 = 0.4  *** we have seen this case before, so we are in an infinite
  37. loop (what is this called, and irrational number?) ***
  38.  
  39. -- 
  40. Donald McLachlan            e-mail    donald.mclachlan@crc.doc.ca
  41. Communications Research Centre / DRX    office    613-998-2845
  42. 3701 Carling Ave.,            fax    613-998-9648
  43. Ottawa, Ontario                labs    613-998-2423 / 613-998-4118
  44. K2H 8S2                    home    613-599-6262
  45. Canada
  46.  
  47.  
  48.  
  49.  
  50.